package com.example.administrator.demo_news;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;



public class MainActivity extends AppCompatActivity {
    private ArrayList<Fragment> fragments=new ArrayList<>();
    private ArrayList<CustomTabEntity> tabEntities=new ArrayList<>();
    private int[] mIconUnselectIds = {
            R.mipmap.unhome,R.mipmap.unperson};
    private int[] mIconSelectIds = {
            R.mipmap.home,R.mipmap.person};
    private String[] mTitles={"首页","我的"};
    private CommonTabLayout commonTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragments.add(new syFragment());
        fragments.add(new xxFragment());
        for (int i=0;i<mTitles.length;i++){
            tabEntities.add(new TabEntity(mTitles[i],mIconSelectIds[i],mIconUnselectIds[i]));
        }
        commonTabLayout=findViewById(R.id.ctl);
        commonTabLayout.setTabData(tabEntities,this,R.id.vp, fragments);


        new Thread(){
            @SuppressLint("NewApi")
            @Override
            public void run() {
                String path="http://106.12.42.150:8080/getsql";
                try {
                    URL url=new URL(path);
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    int code=conn.getResponseCode();
                    if (code==200){
                        Log.i("TEST","SUCCESS");
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        InputStream is=conn.getInputStream();
                        int len=0;
                        byte[] buffer=new byte[1024];
                        while ((len=is.read(buffer))!=-1){
                            baos.write(buffer,0,len);
                        }
                        String result=baos.toString();
                        baos.close();
                        is.close();
                        JSONArray jsonArray=new JSONArray(result);
                        String name="";
                        for (int i=0;i<jsonArray.length();i++){
                            JSONObject jsonObject=jsonArray.getJSONObject(i);
                            name=jsonObject.getString("name");
                            Log.i("TEST","获取到数据了"+name);
                        }
                        Log.i("TEST","获取到数据了"+name);
                    }else {
                        Log.i("TEST","获取内容失败");
                    }
                } catch (Exception e) {
                    Log.i("TEST","连接失败");
                }
            }
        }.start();
    }

}
